# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-web
pkgver=3.9
pkgrel=0
pkgdesc="Web user interface for grommunio"
# s390x: blocked by grommunio-gromox
arch="noarch !riscv64 !s390x"
url="https://grommunio.com/"
license="AGPL-3.0-only"
options="!check" # No test suite
_php=php83
install="$pkgname.pre-install $pkgname.pre-upgrade"
depends="grommunio-gromox
	grommunio-mapi-header-php
	$_php
	$_php-bcmath
	$_php-ctype
	$_php-curl
	$_php-dom
	$_php-gd
	$_php-gettext
	$_php-iconv
	$_php-mbstring
	$_php-openssl
	$_php-session
	$_php-simplexml
	$_php-sodium
	$_php-sqlite3
	$_php-sysvshm
	$_php-zip
	"

makedepends="libxml2-utils $_php-gettext npm gettext"

pkgusers="grommunio groweb"
pkggroups="grommunio groweb"

source="https://github.com/grommunio/grommunio-web/archive/refs/tags/grommunio-web-$pkgver.tar.gz
	0001-makefile.patch
        eslint-deps.patch
	"

builddir="$srcdir/grommunio-web-grommunio-web-$pkgver"

build() {
	make -j1
}

package() {
	# webapp
	install -dm0755 "$pkgdir"/usr/share/webapps/
	cp -rp deploy "$pkgdir"/usr/share/webapps/$pkgname

	# license
	install -Dm644 LICENSE.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# nginx conf
	sed -i "s@/usr/share/grommunio-web/@/usr/share/webapps/grommunio-web/@" build/grommunio-web.conf
	install -Dpm644 build/grommunio-web.conf "$pkgdir"/usr/share/grommunio-common/nginx/locations.d/grommunio-web.conf
	sed -i "s@/php-fpm/@/php-fpm${_php#php}/@" build/grommunio-web-upstream.conf
	install -Dpm644 build/grommunio-web-upstream.conf "$pkgdir"/usr/share/grommunio-common/nginx/upstreams.d/grommunio-web.conf

	# php-fpm
	sed -i "s@/php-fpm/@/php-fpm${_php#php}/@" build/pool-grommunio-web.conf
	install -Dpm644 build/pool-grommunio-web.conf "$pkgdir"/etc/$_php/php-fpm.d/pool-grommunio-web.conf

	# web config
	install -Dm644 config.php.dist "$pkgdir"/etc/grommunio-web/config.php
	ln -sf "/etc/grommunio-web/config.php" "$pkgdir"/usr/share/webapps/grommunio-web/config.php
	rm "$pkgdir"/usr/share/webapps/grommunio-web/config.php.dist
	rm "$pkgdir"/usr/share/webapps/grommunio-web/debug.php.dist

	# plugin config
	for dir in "$pkgdir"/usr/share/webapps/grommunio-web/plugins/*; do
		plugindir=$(basename "$dir")
		if [ -f "$pkgdir"/usr/share/webapps/grommunio-web/plugins/"$plugindir"/config.php ]; then
			mv "$pkgdir"/usr/share/webapps/grommunio-web/plugins/"$plugindir"/config.php "$pkgdir"/etc/grommunio-web/config-"$plugindir".php
			ln -s /etc/grommunio-web/config-"$plugindir".php "$pkgdir"/usr/share/webapps/grommunio-web/plugins/"$plugindir"/config.php
		fi
	done

	# create index and temp directories
	for i in /var/lib/grommunio-web \
		/var/lib/grommunio-web/session \
		/var/lib/grommunio-web/sqlite-index \
		/var/lib/grommunio-web/tmp; do
		install -dm 0770 -g groweb -o groweb "$pkgdir"/$i
	done

	# store version
	echo $pkgver-$pkgrel > "$pkgdir"/usr/share/webapps/grommunio-web/version
}

sha512sums="
bf72a4b3cd8b0e5e8befd5edf0342ce27aa0922d49ace08514b777dd1d85e39e99b14ad65a5c1cdeb30fb1896be709706a0efd3daf6ddfa4e0723a081db93aa9  grommunio-web-3.9.tar.gz
6a9688216d93ccdc48d80489cd4b36a1cba5e0d57d0e8996408b0b04873804f94bef1bc8c2f4c422faa8383589b5f4d30fec5755b0cd3e23739edbeaa1feae54  0001-makefile.patch
93595d31f291158d8afd58c957d73724f6caeb2049bd26bd2126202585aa8b8dec1bf8887dd79ac025c808eba7c97a210f7d114818058b16ad287305a42aab40  eslint-deps.patch
"
